package com.bupt.controller.system;


import com.bupt.annotation.AccessLimit;
import com.bupt.constants.ResponseResult;;
import com.bupt.dto.sys.UserDto;
import com.bupt.exception.BaseException;
import com.bupt.service.system.SysLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "用户 登录 登出操作")
@Slf4j
@RestController
@RequestMapping("/api/sys_user")
public class SysLoginController {

    @Resource
    private SysLoginService sysLoginService;

    @AccessLimit(interfaceName = "用户的登录操作",isLogIn = false)
    @ApiOperation("用户的登录操作")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value = "用户名"),@ApiImplicitParam(name="password",value = "密码")})
    @PostMapping("/login/{username}/{password}")
   public Object Login(@PathVariable("username") String username, @PathVariable("password") String password){
        try {

            Object login = sysLoginService.Login(username, password);
            return ResponseResult.createOK(login);
        } catch (BaseException e) {
            return ResponseResult.createFail(e.getMessage());
        }
    }


    @AccessLimit(interfaceName = "获取当前用户")
    @ApiOperation("获取当前用户")
    @ApiImplicitParams(@ApiImplicitParam(name="token",value = "登录的token"))
    @GetMapping("/getCurrentLoginUser")
  public  Object getCurrentLoginUser(@ApiIgnore UserDto userDto,HttpServletRequest request){
         try{
             Object currentLoginUser = sysLoginService.getCurrentLoginUser(userDto,request);
             return ResponseResult.createOK(currentLoginUser);
         }catch (BaseException e){
            return ResponseResult.createFail(e.getMessage());
         }
  }

    @AccessLimit(interfaceName = "用户登出操作")
    @ApiOperation("用户登出操作")
    @ApiImplicitParams(@ApiImplicitParam(name="token",value = "登录的token"))
    @GetMapping("/quitCurrentUser")
  public  Object quitCurrentUser(@ApiIgnore UserDto userDto,@ApiIgnore HttpServletRequest request){
        try{
            Object o = sysLoginService.quitCurrentUser(userDto,request);
            return ResponseResult.createOK(o);
        }catch (BaseException e){
            return ResponseResult.createFail(e.getMessage());
        }
  }

  @AccessLimit(interfaceName = "获取所有可操作的用户")
  @ApiOperation("获取所有可操作的用户")
  @PostMapping("/getAllUsers")
  public Object getAllUsers(){
       try{
           Object allUsers = sysLoginService.getAllUsers();
           return ResponseResult.createOK(allUsers);
       }catch (BaseException e){
           return ResponseResult.createFail(e.getMessage());
       }
  }

}
